package utils;


import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class correctRaDataUtil {
    /**
     * @param fileName 要处理的文件名
     * @return 返回探空气球的标志#的所在行
     */
    public static List<Integer> getData(String fileName) {
        //首先读取源文件，将数据读出来
        File file = new File(fileName);
        try {
            if (file.isFile() && file.exists()) {
                InputStreamReader read;
                read = new InputStreamReader(new FileInputStream(file));
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt;
                int i = 0;
                List<Integer> mark = new ArrayList<Integer>();
                //先找出#所在的行
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    i = i + 1;
                    if (lineTxt.contains("#")) {
                        mark.add(i);
                    }
                }
                read.close();
                return mark;
            }
        } catch (Exception ignored) {
        }
        return null;
    }

    /**
     * @param fileName          要处理的文件名
     * @param destinationFolder 生成的文件的输出位置
     */
    public static void create(String fileName, String destinationFolder) {
        List<Integer> index = getData(fileName);
        try {
            FileReader in = new FileReader(fileName);
            LineNumberReader reader = new LineNumberReader(in);
            reader.skip(Long.MAX_VALUE);
            int lines = reader.getLineNumber();
            File file = new File(fileName);
            if (file.isFile() && file.exists()) { //判断文件是否存在
                InputStreamReader read = new InputStreamReader(new FileInputStream(file));//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt;
                for (int i = 0; i < index.size(); i++) {
                    ArrayList<String> strings = new ArrayList<String>();
                    int j = 0;
                    if (i < (index.size() - 1)) {
                        for (int i1 = index.get(i); i1 < index.get(i + 1); i1++) {
                            lineTxt = bufferedReader.readLine();
                            //对lineTxt进行判断，如果
                            j = j + 1;
                            if (j == 1) {
                                strings.add(lineTxt);
                            }
                            if (j != 1 && !lineTxt.startsWith("9999", 17)
                                    && !(lineTxt.substring(16, 29).contains("8888"))
                                    && !lineTxt.startsWith("9999", 23)) {
                                String b = lineTxt.substring(16, 27).replace("B", "");
                                String c = b.replace("A", "");
                                strings.add(c);
                            }
                        }
                    }
                    if (i == index.size() - 1) {
                        for (int i1 = index.get(i); i1 < lines + 1; i1++) {
                            lineTxt = bufferedReader.readLine();
                            j = j + 1;
                            if (j == 1) {
                                strings.add(lineTxt);
                            }
                            if (j != 1 && !lineTxt.startsWith("9999", 17)
                                    && !(lineTxt.substring(16, 29).contains("8888"))
                                    && !lineTxt.startsWith("9999", 23)
                            ) {
                                String b = lineTxt.substring(16, 27).replace("B", "");
                                String c = b.replace("A", "");
                                strings.add(c);
                            }
                        }
                    }
                    BufferedWriter bw = new BufferedWriter(new FileWriter
                            (destinationFolder + "/" + i + ".txt"));
                    for (String string : strings) {
                        bw.write(string);
                        bw.newLine();
                        bw.flush();
                    }
                    bw.close();
                }
                read.close();
            }
        } catch (Exception ignored) {

        }
    }
}
